<template>
	<!-- 随堂笔记详情 -->
	<view class="cs-detl-x">
		<view class="cs-detl">
			<view class="cs-detl-tc">
				<input class="tc-input" type="text" v-model="detailData.notes_title" :disabled="!isUtil"/>
			</view>
			<view class="cs-detl-editor-x">
				<view class='toolbar' @tap="format" v-if="isUtil">
					<view>
						<view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont iconziti icon_caozuo"
						 data-name="backgroundColor" data-value="#00ff00"></view>
					</view>
					<view><view class="iconfont iconduigou1 icon_caozuo" data-name="list" data-value="check"></view></view>
					<view><view class="iconfont iconshangyibu icon_caozuo" @tap="undo"></view></view>
					<view><view class="iconfont iconxiayibu icon_caozuo" @tap="redo"></view></view>
					<!-- 手/笔 -->
					<view><view class="iconfont iconshouxieqianpi icon_caozuo" @tap="clickOpen"></view></view>
				</view>
				<editor id="editor" class="ql-container" :read-only='!isUtil'  placeholder="开始输入..." showImgSize showImgToolbar showImgResize
				 @statuschange="onStatusChange" @ready="onEditorReady">
				</editor>
			</view>
			<!-- 手/笔 和草稿 -->
			<v-open v-if='isOpen' @changeOpen='changeOpen' :is_top='false'></v-open>
			<v-draw v-if='isDraw' @clickDraw='clickDraw'></v-draw>
		</view>
	</view>
</template>

<script>
	import open from '@/components/note/Base-open';
	import draw from '@/components/note/Base-draw';
	export default {
		components: {
			vOpen: open,
			vDraw: draw
		},
		// 原生导航栏 修改/保存事件 TODO: 暂时不需要，所以已关原生导航栏标题
		onNavigationBarButtonTap(e) {
			if (this.detailData.notes_title.trim().length <= 0) {
				return uni.showToast({title: '请输入标题', icon: 'none'})
			}
			// 获取webview对象
			var webView = this.$mp.page.$getAppWebview();
			let title = this.isUtil ? '修改' : '保存';
			// 改变导航栏内容状态
			webView.setTitleNViewButtonStyle(0, {text: title})
			if (this.isUtil) {
				uni.showLoading({
					title: '保存中...'
				})
				// 获取富文本内容，并进行保存
				var _this = this;
				this.editorCtx.getContents({
					success: function(res) {
						var content = res.html.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
						let obj = {
							usr_id: _this.stuId,
							notes_title: _this.detailData.notes_title,
							notes_content: content,
							course_key: _this.detailData.course_key,
							notes_type: 3,
							unid: _this.detailData.unid
						}
						_this.$kbapi.apiPost('mobile/notes/save', obj).then(res => {
							if (res.data.code == 200) {
								uni.showToast({title: '修改成功'})
								// 1秒后进行后退
								setTimeout(() => {
									uni.navigateBack()
								}, 1000)
							} else {
								uni.showToast({title: res.data.message, icon: 'none'})
							}
						})
					}
				});
			}
			this.isUtil = !this.isUtil;
		},
		data() {
			return {
				noteType: uni.getStorageSync('noteType'), //1家长 2孩子 
				stuId: uni.getStorageSync('userInfo').id, //个人ID
				detailData: {}, // 笔记详情数据
				unid: null, // 笔记ID
				iconArr: [ // 工具数组
					{icon: 'iconziti', title: '字体'},
					{icon: 'iconduigou1' , title: '对勾'},
					{icon: 'iconshouxieqianpi', title: '手写签批'},
				],
				formats: {},
				
				isOpen: false, // 手/笔开关
				isDraw: false, // 画布开关
				
				isUtil: false, // 切换修改和保存的状态
			}
		},
		// 离开时清空画布缓存
		onBackPress() {
			uni.removeStorageSync('imgbase')
		},
		onLoad(e) {
			this.unid = e.unid;
			if(this.noteType == 2) {
				this.stuId = uni.getStorageSync('currentChild').student_id;
				var webView = this.$mp.page.$getAppWebview();
				webView.setTitleNViewButtonStyle(0,{
					text: '',
					width: 0
				})
			}
			this.getDetail();
		},
		methods: {
			// 获取笔记详情
			getDetail() {
				this.$kbapi.apiPost('mobile/notes/info/'+this.unid,{unid:this.unid,usr_id:this.stuId}).then(res=>{
					if(res.data.code == 200 && res.data.data.unid) {
						this.detailData = res.data.data;
						uni.setNavigationBarTitle({
							title: this.detailData.notes_title
						})
						var _this = this;
						setTimeout(()=>{
							_this.onEditorReady();
						},500)
					}
				})
			},
			/* 编辑器初始化 && 设置富文本框初始值 */
			onEditorReady() {
				var _this = this;
				uni.createSelectorQuery().select('#editor').context((res) => {
					_this.editorCtx = res.context;
					// 解析返回过来的富文本并渲染
					_this.editorCtx.setContents({
						html: _this.detailData.notes_content
					})
				}).exec()
			},
			/* 撤销至上一步 */
			undo() {
				this.editorCtx.undo()
			},
			/* 恢复至下一步 */
			redo() {
				this.editorCtx.redo()
			},
			/* 清除当前选区的样式 */
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			/* 获取富文本支持设置的样式 */
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				this.editorCtx.format(name, value)
			},
			// 监听富文本
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			// 启动/隐藏手/笔
			clickOpen() {
				this.isOpen = !this.isOpen
			},
			// 手笔选择事件
			changeOpen(title) {
				if (title === 'hand') { // 点击了手
					this.isDraw = true // 启动画布
					this.isOpen = false // 关闭手笔两个按钮
				}
				if (title === 'pen') { // 点击了笔
					uni.showToast({title: '暂未开通~', icon: 'none'})
				}
			},
			// 画布点击事件
			clickDraw(title) {
				if (title === 'save') {
					var img = uni.getStorageSync('imgbase');
					// 保存后开始插入图片，并且隐藏画布
					this.isDraw = false
					this.editorCtx.insertImage({
						src: uni.getStorageSync('imgbase'),
						alt: '图像',
						success: function() {
							uni.removeStorageSync('imgbase')
						} 
					})
				} else if (title === 'close') {
					this.isDraw = false
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}
	// 输入标题
	.cs-detl-tc{
		padding: 20upx 30upx;
		background: #FFFFFF;
	}
	.tc-input{
		font-size: 34upx;
		color: #333333;
	}
	
	// 富文本
	.cs-detl-editor-x{
		padding-bottom: 100upx;
	}
	.toolbar{
		display: flex;
		position: fixed;
		bottom: 0;left: 0;right: 0;
		justify-content: space-around;
		z-index: 1;
		text-align: center;
		font-size: 32upx;
		color: #666666;
		box-shadow:0px -2px 4px rgba(0,0,0,0.16);
		background: #FFFFFF;
	}
	.ql-container{
		box-sizing: border-box;
		width: 100%;
		min-height: 50vh;
		height: auto;
		padding: 0 20upx 200upx;
		& img {
			max-width: 80%;
			height: auto;
		}
	}
	
	// 图标
	.icon_caozuo{
		color: #000000;
		font-size: 50upx;
	}
</style>
